package com.newhero.product.module.sjzsw.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.mybatis.toolkit.IdWorker;
import com.newhero.product.module.sjzsw.persistence.entity.WebBaseInfo;
import com.newhero.product.module.sjzsw.persistence.entity.WebOrgan;
import com.newhero.product.module.sjzsw.service.IWebBaseInfoService;
import com.newhero.product.module.sjzsw.service.IWebOrganService;

 /**
 * @ClassName: WebBaseInfoController
 * @Description: 访问控制类
 * @author liangzhuoxin
 * @date 2019-01-28
 */
@Controller
@RequestMapping("/docInfoService")
public class WebBaseInfoController extends BaseController {
    
    @Autowired 
    private IWebBaseInfoService webBaseInfoServiceImpl;
    	
    @Autowired 
    private IWebOrganService webOrganServiceImpl;
    /**
     * 列表页面
     * @return
     * @throws IOException 
     */    
	@RequestMapping(value = "/docInfoSend", method ={RequestMethod.GET,RequestMethod.POST })
	public void webBaseInfoList(
			HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		{

			
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/plain; charset=utf-8");
			JSONObject jsonObject=new JSONObject();
			PrintWriter out = response.getWriter();
			String str=request.getParameter("str");
			if(str!=null&&!str.equals("")){
				System.out.println("收到的信息："+str);
				String qrcodeId = "";
				try {
					JSONObject object=JSONObject.fromString(str.toString());
					System.out.println("请求IP："+request.getRemoteAddr());
					
					String account = object.getString("account");
					String password = object.getString("password");
					int flag = 0;
					EntityWrapper<WebOrgan> organ = new EntityWrapper<WebOrgan>();
					organ.eq("account", account);
					organ.eq("passwd", password);
					flag = webOrganServiceImpl.selectCount(organ);
					
					if(flag > 0){
						String ywid = object.getString("ywid");
						String ywextid = object.getString("ywextid");
						String ywtype = object.getString("ywtype");
						String infokind = object.getString("infokind");
						String infotitle = object.getString("infotitle");
						String infofwjg = object.getString("infofwjg");
						String infoyear = object.getString("infoyear");
						String infojjcd = object.getString("infojjcd");
						String infolxr = object.getString("infolxr");
						String infolxdh = object.getString("infolxdh");
						String cuid = object.getString("cuid");
						String cuname = object.getString("cuname");
						String coid = object.getString("coid");
						String coname = object.getString("coname");
						String ctime = object.getString("ctime");
						String qrcode = object.getString("qrcode");
						String toid = object.getString("toid");
						String toname = object.getString("toname"); 
						WebBaseInfo webBaseInfo = new WebBaseInfo();
						
						webBaseInfo.setRid(IdWorker.getId());
						webBaseInfo.setAccount(account);
						webBaseInfo.setYwid(ywid);
						webBaseInfo.setYwtype(ywtype);
						//涉及到主送和抄送时，存储主送数据主键，用来保证二维码数据唯一
						if("0".equals(ywtype)){
							webBaseInfo.setYwextid(ywextid);
						}
						webBaseInfo.setInfoFullkind(infokind);
						webBaseInfo.setInfoTitle(infotitle);
						webBaseInfo.setInfoFwjg(infofwjg);
						int infoYear = Integer.parseInt(infoyear);
						webBaseInfo.setInfoYear(infoYear);
						webBaseInfo.setInfoJjcd(infojjcd);
						webBaseInfo.setInfoLxr(infolxr);
						webBaseInfo.setInfoLxdh(infolxdh);
						webBaseInfo.setCreateUserId(cuid);
						webBaseInfo.setCreateUserName(cuname);
						webBaseInfo.setCreateOrganId(coid);
						webBaseInfo.setCreateOrganName(coname);
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");					
						webBaseInfo.setCreateTime(df.format(new Date()));
						webBaseInfo.setCtime(df.format(new Date()));
						
						if(qrcode != null && !"".equals(qrcode)){
							qrcodeId = qrcode;
							webBaseInfo.setQrcode(qrcodeId);
						}else{
							qrcodeId = IdWorker.getId();
							webBaseInfo.setQrcode(qrcode);
						}
						webBaseInfo.setToOrganId(toid);
						webBaseInfo.setToOrganName(toname);
						boolean isSuccess = false;
						isSuccess = webBaseInfoServiceImpl.insert(webBaseInfo);
						if(isSuccess){
							jsonObject.put("code", 1);
							jsonObject.put("data", qrcodeId);
							jsonObject.put("msg", "成功");						
						}else{
							jsonObject.put("code", 0);
							jsonObject.put("msg", "失败");
						}
					}else {
						jsonObject.put("code", -1);
						jsonObject.put("msg", "账号密码错误");
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					System.out.println(e);
					jsonObject.put("code", 0);
					jsonObject.put("msg", "失败");
				}
			}else {
				//空的参数
				jsonObject.put("code", 0);
				jsonObject.put("msg", "参数为空");
			}
			System.out.println("返回信息："+jsonObject);
			out.print(jsonObject.toString());
			out.flush();
			out.close();
			
			
		}
		
	}
}
